home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Tracker 4.0 / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  13.3 KB  |  598 lines  |  [TEXT/KAHL]

  1. /* commands.c */
  2.  
  3. /* $Id: commands.c,v 4.0 1994/01/11 17:44:07 espie Exp espie $
  4.  * $Log: commands.c,v $
  5.  * Revision 4.0  1994/01/11  17:44:07  espie
  6.  * Abstracted IO.
  7.  *
  8.  * Revision 1.6  1994/01/09  17:36:22  Espie
  9.  * Generalized open.c.
  10.  *
  11.  * Revision 1.5  1994/01/08  02:04:21  Espie
  12.  * Some notice to status.
  13.  *
  14.  * Revision 1.4  1994/01/05  14:54:09  Espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 1.3  1994/01/05  13:50:43  Espie
  18.  * Cosmetic change.
  19.  *
  20.  * Revision 1.2  1993/12/28  13:54:44  Espie
  21.  * Use notice.
  22.  *
  23.  * Revision 1.1  1993/12/26  00:55:53  Espie
  24.  * Initial revision
  25.  *
  26.  * Revision 3.15  1993/12/04  16:12:50  espie
  27.  * Lots of LOCAL added.
  28.  *
  29.  * Revision 3.14  1993/11/17  15:31:16  espie
  30.  * play_note instead of ch->mode.
  31.  *
  32.  * Revision 3.13  1993/08/04  11:34:33  espie
  33.  * *** empty log message ***
  34.  *
  35.  * Revision 3.12  1993/07/18  10:39:44  espie
  36.  * Cleaned up.
  37.  *
  38.  * Revision 3.11  1993/07/17  22:23:41  espie
  39.  * Fixed bug with bad loops.
  40.  *
  41.  * Revision 3.9  1993/05/09  14:06:03  espie
  42.  * Modified the way set_speed works.
  43.  *
  44.  * Revision 3.8  1993/04/28  20:13:13  espie
  45.  * Very small bug with volume (Lawrence).
  46.  *
  47.  * Revision 3.7  1993/01/15  14:00:28  espie
  48.  * Added bg/fg test.
  49.  *
  50.  * Revision 3.6  1992/11/27  10:29:00  espie
  51.  * General cleanup
  52.  *
  53.  * Revision 3.5  1992/11/24  10:51:19  espie
  54.  * More precise vibrato table.
  55.  *
  56.  * Revision 3.4  1992/11/23  10:12:23  espie
  57.  * *** empty log message ***
  58.  *
  59.  * Revision 3.3  1992/11/22  17:20:01  espie
  60.  * Simplified delay_pattern.
  61.  *
  62.  * Revision 3.2  1992/11/20  14:53:32  espie
  63.  * Added finetune.
  64.  *
  65.  * Revision 3.1  1992/11/19  20:44:47  espie
  66.  * Protracker commands.
  67.  *
  68.  * Revision 3.0  1992/11/18  16:08:05  espie
  69.  * New release.
  70.  *
  71.  * Revision 2.12  1992/11/13  13:24:24  espie
  72.  * Added some extended commands: E12AB, and some.
  73.  * now use set_volume in audio.c. All the device-dependent operation
  74.  * is there.
  75.  * Defensive programming: check the range of each note
  76.  * for arpeggio setup.
  77.  * Structured part of the code, especially replay ``automaton''
  78.  * and setting up of effects.
  79.  *
  80.  * Revision 1.9  1991/11/17  17:09:53  espie
  81.  * Added missing prototypes.
  82.  * Dynamic oversample and frequency.
  83.  * Added arpeggio.
  84.  * Fixed up vibrato depth.
  85.  * Added vibslide and portaslide.
  86.  * Added command 9.
  87.  */
  88.  
  89. #include <stdio.h>
  90.  
  91. #include "defs.h"
  92. #include "channel.h"
  93. #include "song.h"
  94. #include "extern.h"
  95.      
  96. ID("$Id: commands.c,v 4.0 1994/01/11 17:44:07 espie Exp espie $")
  97.  
  98. /* sine table for the vibrato effect (obtained through build.c) */
  99.  
  100. int vibrato_table[64] = 
  101.    {
  102.    0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  103.    510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  104.    -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  105.    -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  106.    -195,-148,-99,-49
  107.    };
  108.  
  109. /***
  110.  *
  111.  *
  112.  * setting up effects/doing effects.
  113.  * The set_xxx gets called while parsing the effect,
  114.  * the do_xxx gets called each tick, and update the
  115.  * sound parameters while playing it.
  116.  *
  117.  *
  118.  ***/
  119.  
  120.  
  121. void do_nothing(ch)
  122. struct channel *ch;
  123.    {
  124.    }
  125.  
  126. LOCAL void set_nothing(a, ch)
  127. struct automaton *a;
  128. struct channel *ch;
  129.    {
  130.    }
  131.  
  132. /* slide pitch (up or down) */
  133. LOCAL void do_slide(ch)
  134. struct channel *ch;
  135.    {
  136.    ch->pitch += ch->slide;
  137.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  138.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  139.    set_current_pitch(ch, ch->pitch);
  140.    }
  141.  
  142. LOCAL void set_upslide(a, ch)
  143. struct automaton *a;
  144. struct channel *ch;
  145.    {
  146.    ch->adjust = do_slide;
  147.    if (a->para)
  148.       ch->slide = a->para;
  149.    }
  150.  
  151. LOCAL void set_downslide(a, ch)
  152. struct automaton *a;
  153. struct channel *ch;
  154.    {
  155.    ch->adjust = do_slide;
  156.    if (a->para)
  157.       ch->slide = -a->para;
  158.    }
  159.  
  160. /* modulating the pitch with vibrato */
  161. LOCAL void do_vibrato(ch)
  162. struct channel *ch;
  163.    {
  164.    int offset;
  165.  
  166.       /* this is no longer a literal transcription of the pt
  167.        * code. I have rescaled the vibrato table.
  168.        */
  169.    ch->viboffset += ch->vibrate;
  170.    ch->viboffset &= 63;
  171.       /* please don't use logical shift on signed values */
  172.    offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  173.       /* temporary update of only the step value,
  174.        * note that we do not change the saved pitch.
  175.        */
  176.    set_current_pitch(ch, ch->pitch + offset);
  177.    }
  178.  
  179. LOCAL void set_vibrato(a, ch)
  180. struct automaton *a;
  181. struct channel *ch;
  182.    {
  183.    ch->adjust = do_vibrato;
  184.    if (HI(a->para))
  185.       ch->vibrate = HI(a->para);
  186.    if (LOW(a->para))
  187.       ch->vibdepth = LOW(a->para);
  188.    }
  189.  
  190. /* arpeggio looks a bit like chords: we alternate between two
  191.  * or three notes very fast.
  192.  */
  193. LOCAL void do_arpeggio(ch)
  194. struct channel *ch;
  195.    {
  196.    if (++ch->arpindex >= MAX_ARP)
  197.       ch->arpindex =0;
  198.    set_current_pitch(ch, ch->arp[ch->arpindex]);
  199.    }
  200.  
  201. LOCAL void set_arpeggio(a, ch)
  202. struct automaton *a;
  203. struct channel *ch;
  204.    {
  205.       /* arpeggio can be installed relative to the
  206.        * previous note, so we have to check that there
  207.        * actually is a current(previous) note
  208.        */
  209.    if (ch->note == NO_NOTE)
  210.       {
  211.       status("No note present for arpeggio");
  212.       error = FAULT;
  213.       }
  214.    else
  215.       {
  216.       int note;
  217.  
  218.       ch->arp[0] = pitch_table[ch->note][ch->finetune];
  219.       note = ch->note + HI(a->para);
  220.       if (note < NUMBER_NOTES)
  221.          ch->arp[1] = pitch_table[note][ch->finetune];
  222.       else
  223.          {
  224.          status("Arpeggio note out of range");
  225.          error = FAULT;
  226.          }
  227.       note = ch->note + LOW(a->para);
  228.       if (note < NUMBER_NOTES)
  229.          ch->arp[2] = pitch_table[note][ch->finetune];
  230.       else
  231.          {
  232.          status("Arpeggio note out of range");
  233.          error = FAULT;
  234.          }
  235.       ch->arpindex = 0;
  236.       ch->adjust = do_arpeggio;
  237.       }
  238.    }
  239.  
  240. /* volume slide. Mostly used to simulate waveform control.
  241.  * (attack/decay/sustain).
  242.  */
  243. LOCAL void do_slidevol(ch)
  244. struct channel *ch;
  245.    {
  246.    set_current_volume(ch, ch->volume + ch->volumerate);
  247.    }
  248.  
  249. /* note that volumeslide does not have a ``take default''
  250.  * behavior. If para is 0, this is truly a 0 volumeslide.
  251.  * Issue: is the test really necessary ? Can't we do
  252.  * a HI(para) - LOW(para). Answer: protracker does not.
  253.  */
  254. LOCAL void parse_slidevol(ch, para)
  255. struct channel *ch;
  256. int para;
  257.    {
  258.    if (LOW(para))
  259.       ch->volumerate = -LOW(para);
  260.    else
  261.       ch->volumerate = HI(para);
  262.    }
  263.  
  264. LOCAL void set_slidevol(a, ch)
  265. struct automaton *a;
  266. struct channel *ch;
  267.    {
  268.    ch->adjust = do_slidevol;
  269.    parse_slidevol(ch, a->para);
  270.    }
  271.  
  272. /* portamento: gets from a given pitch to another.
  273.  * We can simplify the routine by cutting it in
  274.  * a pitch up and pitch down part while setting up
  275.  * the effect.
  276.  */
  277. LOCAL void do_portamento(ch)
  278. struct channel *ch;
  279.    {
  280.    if (ch->pitch < ch->pitchgoal)
  281.       {
  282.       ch->pitch += ch->pitchrate;
  283.       ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  284.       }
  285.    else if (ch->pitch > ch->pitchgoal)
  286.       {
  287.       ch->pitch -= ch->pitchrate;
  288.       ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  289.       }
  290.       /* if we want to implement funk glissando, we need a change right
  291.        * there
  292.        */
  293.    set_current_pitch(ch, ch->pitch);
  294.    }
  295.  
  296. /* if para and pitch are 0, this is obviously a continuation
  297.  * of the previous portamento.
  298.  */
  299. LOCAL void set_portamento(a, ch)
  300. struct automaton *a;
  301. struct channel *ch;
  302.    {
  303.    ch->adjust = do_portamento;
  304.    if (a->para)
  305.       ch->pitchrate = a->para;
  306.    if (a->pitch)
  307.       ch->pitchgoal = a->pitch;
  308.    }
  309.  
  310. /*
  311.  * combined commands.
  312.  */
  313. LOCAL void do_portaslide(ch)
  314. struct channel *ch;
  315.    {
  316.    do_portamento(ch);
  317.    do_slidevol(ch);
  318.    }
  319.  
  320. LOCAL void set_portaslide(a, ch)
  321. struct automaton *a;
  322. struct channel *ch;
  323.    {
  324.    ch->adjust = do_portaslide;
  325.    if (a->pitch)
  326.       ch->pitchgoal = a->pitch;
  327.    parse_slidevol(ch, a->para);
  328.    }
  329.  
  330. LOCAL void do_vibratoslide(ch)
  331. struct channel *ch;
  332.    {
  333.    do_vibrato(ch);
  334.    do_slidevol(ch);
  335.    }
  336.  
  337. LOCAL void set_vibratoslide(a, ch)
  338. struct automaton *a;
  339. struct channel *ch;
  340.    {
  341.    ch->adjust = do_vibratoslide;
  342.    parse_slidevol(ch, a->para);
  343.    }
  344.  
  345. /***
  346.  *
  347.  *  effects that just need a setup part
  348.  *
  349.  ***/
  350.  
  351. /* IMPORTANT: because of the special nature of
  352.  * the player, we can't process each effect independently,
  353.  * we have to merge effects from the four channel before
  354.  * doing anything about it. For instance, there can be 
  355.  * several speed change in the same note.
  356.  */
  357. LOCAL void set_speed(a, ch)
  358. struct automaton *a;
  359. struct channel *ch;
  360.    {
  361.    if (a->para >= 32)
  362.       {
  363.       a->new_finespeed = a->para;
  364.       a->do_stuff |= SET_FINESPEED;
  365.       }
  366.    else if (a->para)
  367.       {
  368.       a->new_speed = a->para;
  369.       a->do_stuff |= SET_SPEED;
  370.       }
  371.    }
  372.  
  373. LOCAL void set_skip(a, ch)
  374. struct automaton *a;
  375. struct channel *ch;
  376.    {
  377.       /* BCD decoding in read.c */
  378.    a->new_note = a->para;
  379.    a->do_stuff |= SET_SKIP;
  380.    }
  381.  
  382. LOCAL void set_fastskip(a, ch)
  383. struct automaton *a;
  384. struct channel *ch;
  385.    {
  386.    a->new_pattern = a->para;
  387.    a->do_stuff |= SET_FASTSKIP;
  388.    }
  389.  
  390. /* immediate effect: starts the sample somewhere
  391.  * off the start.
  392.  */
  393. LOCAL void set_offset(a, ch)
  394. struct automaton *a;
  395. struct channel *ch;
  396.    {
  397.    set_position(ch, a->para * 256);
  398.    }
  399.  
  400. /* change the volume of the current channel.
  401.  * Is effective until there is a new set_volume,
  402.  * slide_volume, or an instrument is reloaded 
  403.  * explicitly by giving its number. Obviously, if
  404.  * you load an instrument and do a set_volume in the
  405.  * same note, the set_volume will take precedence.
  406.  */
  407. LOCAL void set_volume(a, ch)
  408. struct automaton *a;
  409. struct channel *ch;
  410.    {
  411.    set_current_volume(ch, a->para);
  412.    }
  413.  
  414.  
  415.  
  416. /***
  417.  *
  418.  * EXTENDED COMMANDS
  419.  *
  420.  ***/
  421.  
  422. /* extended command: retrig note at a fast pace
  423.  */
  424. LOCAL void do_retrig(ch)
  425. struct channel *ch;
  426.    {
  427.    if (--ch->current <= 0)
  428.       {
  429.       reset_note(ch, ch->note, ch->pitch);
  430.       ch->current = ch->retrig;
  431.       }
  432.    }
  433.  
  434. LOCAL void set_retrig(a, ch)
  435. struct automaton *a;
  436. struct channel *ch;
  437.    {
  438.    ch->retrig = a->para;
  439.    ch->current = ch->retrig;
  440.    ch->adjust = do_retrig;
  441.    }
  442.  
  443. /* extended command: start note after a small
  444.  * delay
  445.  */
  446. LOCAL void do_latestart(ch)
  447. struct channel *ch;
  448.    {
  449.    if (--ch->current <= 0)
  450.       {
  451.       reset_note(ch, ch->note, ch->pitch);
  452.       ch->adjust = do_nothing;
  453.       }
  454.    }
  455.  
  456. LOCAL void set_late_start(a, ch)
  457. struct automaton *a;
  458. struct channel *ch;
  459.    {
  460.    play_note(ch->audio, NULL, 0);
  461.    ch->current = a->para;
  462.    ch->adjust = do_latestart;
  463.    }
  464.  
  465. /* extended command: cut note after some time.
  466.  * Note we only kill the volume, as protracker does...
  467.  */
  468. LOCAL void do_cut(ch)
  469. struct channel *ch;
  470.    {
  471.    if (ch->retrig)
  472.       {
  473.       if (--ch->retrig == 0)
  474.          set_current_volume(ch, 0);
  475.       }
  476.    }
  477.  
  478. LOCAL void set_note_cut(a, ch)
  479. struct automaton *a;
  480. struct channel *ch;
  481.    {
  482.    ch->retrig = a->para;
  483.    ch->adjust = do_cut;
  484.    }
  485.  
  486.  
  487. LOCAL void set_smooth_up(a, ch)
  488. struct automaton *a;
  489. struct channel *ch;
  490.    {
  491.    ch->pitch += a->para;
  492.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  493.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  494.    set_current_pitch(ch, ch->pitch);
  495.    }
  496.  
  497. LOCAL void set_smooth_down(a, ch)
  498. struct automaton *a;
  499. struct channel *ch;
  500.    {
  501.    ch->pitch -= a->para;
  502.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  503.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  504.    set_current_pitch(ch, ch->pitch);
  505.    }
  506.  
  507. LOCAL void set_change_finetune(a, ch)
  508. struct automaton *a;
  509. struct channel *ch;
  510.    {
  511.    ch->finetune = a->para;
  512.    }
  513.  
  514.  
  515. LOCAL void set_loop(a, ch)
  516. struct automaton *a;
  517. struct channel *ch;
  518.    {
  519.       /* Note: the current implementation of protracker
  520.        * does not allow for a jump from pattern to pattern,
  521.        * even though it looks like a logical extension to the current 
  522.        * format.
  523.        */
  524.    if (a->para == 0) 
  525.       a->loop_note_num = a->note_num;
  526.    else
  527.       {
  528.       if (a->loop_counter == 0)
  529.          a->loop_counter = a->para + 1;
  530.       /* We have to defer the actual count-down and note jump
  531.        * to automaton.c, because some modules include several
  532.        * loops on the same measure, which is a bit confusing
  533.        * (see don't you want me)
  534.        */
  535.       a->do_stuff |= JUMP_PATTERN;
  536.       }
  537.    }
  538.  
  539. LOCAL void set_smooth_upvolume(a, ch)
  540. struct automaton *a;
  541. struct channel *ch;
  542.    {
  543.    set_current_volume(ch, ch->volume + a->para);
  544.    }
  545.  
  546. LOCAL void set_smooth_downvolume(a, ch)
  547. struct automaton *a;
  548. struct channel *ch;
  549.    {
  550.    set_current_volume(ch, ch->volume - a->para);
  551.    }
  552.  
  553.  
  554. LOCAL void set_delay_pattern(a, ch)
  555. struct automaton *a;
  556. struct channel *ch;
  557.    {
  558.    a->counter -= (a->para + 1) * a->speed;
  559.    a->do_stuff |= DELAY_PATTERN;
  560.    }
  561.  
  562.  
  563.  
  564. /* Initialize the whole effect table */
  565.  
  566. void init_effects(table)
  567. void (*table[]) P((struct automaton *a, struct channel *ch));
  568.    {
  569.    int i;
  570.  
  571.    for (i = 0; i < NUMBER_EFFECTS; i++)
  572.       table[i] = set_nothing;
  573.    table[EFF_ARPEGGIO] = set_arpeggio;
  574.    table[EFF_SPEED] = set_speed;
  575.    table[EFF_SKIP] = set_skip;
  576.    table[EFF_FF] = set_fastskip;
  577.    table[EFF_VOLUME] = set_volume;
  578.    table[EFF_VOLSLIDE] = set_slidevol;
  579.    table[EFF_OFFSET] = set_offset;
  580.    table[EFF_PORTA] = set_portamento;
  581.    table[EFF_PORTASLIDE] = set_portaslide;
  582.    table[EFF_UP] = set_upslide;
  583.    table[EFF_DOWN] = set_downslide;
  584.    table[EFF_VIBRATO] = set_vibrato;
  585.    table[EFF_VIBSLIDE] = set_vibratoslide;
  586.    table[EFF_SMOOTH_UP] = set_smooth_up;
  587.    table[EFF_SMOOTH_DOWN] = set_smooth_down;
  588.    table[EFF_CHG_FTUNE] = set_change_finetune;
  589.    table[EFF_LOOP] = set_loop;
  590.    table[EFF_RETRIG] = set_retrig;
  591.    table[EFF_S_UPVOL] = set_smooth_upvolume;
  592.    table[EFF_S_DOWNVOL] = set_smooth_downvolume;
  593.    table[EFF_NOTECUT] = set_note_cut;
  594.    table[EFF_LATESTART] = set_late_start;
  595.    table[EFF_DELAY] = set_delay_pattern;
  596.    }
  597.  
  598.